package com.enjoy.studyfragment.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.enjoy.studyfragment.R;
import com.enjoy.studyfragment.adapter.ContactAdapter;
import com.enjoy.studyfragment.bean.CityBean;
import com.enjoy.studyfragment.widget.contact.SearchView;
import com.enjoy.studyfragment.widget.contact.SideBar;
import com.mcxtzhang.indexlib.suspension.SuspensionDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/3/16.
 */

public class ContactFragment extends TopBarFragment {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.sideBar)
    SideBar sideBar;
    @BindView(R.id.tvSideBarHint)
    TextView tvSideBarHint;
    @BindView(R.id.searchView)
    SearchView searchView;
    @BindView(R.id.noResult)
    TextView noResult;

    private static final String INDEX_STRING_TOP = "↑";
    private List<CityBean> mDatas = new ArrayList<>();
    private List<CityBean> filterDatas = new ArrayList<>();
    private ContactAdapter mAdapter;

    @Override
    public Object setLayout() {
        return R.layout.fragment_contact;
    }

    @Override
    public void init() {
        super.init();
        initRecycler();
    }

    public void initRecycler(){
        LinearLayoutManager mManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(mManager);
        SuspensionDecoration mDecoration = new SuspensionDecoration(getContext(), mDatas);
        recyclerView.addItemDecoration(mDecoration);
        //如果add两个，那么按照先后顺序，依次渲染。
//        recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL));
        //indexbar初始化
        sideBar.setmPressedShowTextView(tvSideBarHint)//设置HintTextView
                .setNeedRealIndex(true)//设置需要真实的索引
                .setmLayoutManager(mManager);//设置RecyclerView的LayoutManager

        mDatas = new ArrayList<>();
        String[] data = getResources().getStringArray(R.array.provinces);
        //微信的头部 也是可以右侧IndexBar导航索引的，
        // 但是它不需要被ItemDecoration设一个标题titile
        mDatas.add((CityBean) new CityBean("新的朋友").setTop(true).setBaseIndexTag(INDEX_STRING_TOP));
        mDatas.add((CityBean) new CityBean("群聊").setTop(true).setBaseIndexTag(INDEX_STRING_TOP));
        mDatas.add((CityBean) new CityBean("标签").setTop(true).setBaseIndexTag(INDEX_STRING_TOP));
        mDatas.add((CityBean) new CityBean("公众号").setTop(true).setBaseIndexTag(INDEX_STRING_TOP));
        for (int i = 0; i < data.length; i++) {
            CityBean cityBean = new CityBean();
            cityBean.setCity(data[i]);//设置城市名称
            mDatas.add(cityBean);
        }

        mAdapter = new ContactAdapter();
        mAdapter.setNewData(mDatas);
        recyclerView.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();

        sideBar.setmSourceDatas(mDatas)//设置数据
                .invalidate();
        mDecoration.setmDatas(mDatas);

        searchView.setCallback(new SearchView.ResultCallback(){
            @Override
            public void result(String s) {
                filterData(s);
            }

            @Override
            public void cancle() {
                hideSoftInput();
            }
        });
    }

    /**
     * 首字符过滤
     * @param s
     */
    private void filterData(String s) {
        filterDatas.clear();
        if (TextUtils.isEmpty(s)) {
            filterDatas.addAll(mDatas);
        } else {
            CityBean tempInfoBean;
            int listSize = mDatas.size();
            for(int i = 0; i < listSize; i ++){
                tempInfoBean = mDatas.get(i);
                String userName = tempInfoBean.getCity();
                if(userName.startsWith(s.substring(0,1))){
                    filterDatas.add(tempInfoBean);
                }
            }
        }
        //放置没有搜索结果的图片
        if(filterDatas.size() == 0){
            noResult.setVisibility(View.VISIBLE);
        }else {
            noResult.setVisibility(View.GONE);
        }
        mAdapter.setNewData(filterDatas);
        mAdapter.notifyDataSetChanged();
    }

}
